﻿(function () {
    'use strict';
    angular.module('app').factory('loginSrvc', ['$http', '$q', 'apiresource',
        function ($http, $q, apiresource) {

        return {
            login: login,
            setToken: setToken
        };

        function login(data) {
            var deferred = $q.defer();
            //var url = config.rootUrl + 'Token';
            var url = apiresource.Token;
            $http({ method: 'POST', url: url, data: data })
                .success(function(responsedata) {
                    deferred.resolve(responsedata);
                })
                .error(function (responsedata, status) {
                    deferred.reject(data,status);
                });
            return deferred.promise;
        }
            
        function setToken(data) {
            var deferred = $q.defer();
            var url = apiresource.SetToken;
            var headers = {Authorization: 'Bearer ' + data.Token};
            $http({ method: 'POST', url: url, data: data, headers: headers })
                .success(function (responsedata) {
                    deferred.resolve(responsedata);
                })
                .error(function (responsedata, status) {
                    deferred.reject(responsedata, status);
                });
            return deferred.promise;
        }

    }]);
})();